rm(list = setdiff(ls(), lsf.str()))

load("Study 1/Altered Data/Study1_Spain.Rdata")

### Correlation matrix with---------------------
cor_ivs <- data[c("A_critical", "open", "con", "ext", "neu" , "lr_placement", "cynicism")]
names(cor_ivs) <- c("1. Agreeableness","2. Opennesss","3. Conscientiousness",
                    "4. Extraversion", "5. Neuroticism", 
                    "6. Left-Right ideology", "7. Cynicism")
correlation.matrix <- cor(cor_ivs, use="complete.obs")
correlation.matrix <- data.frame(get_lower_tri(correlation.matrix))
correlation.matrix<-correlation.matrix[,c(1:7)]
names(correlation.matrix) <- seq(1,7,1)
correlation.matrix<-as.matrix(correlation.matrix)
stargazer(correlation.matrix, title="Spanish Election Study: Correlation Matrix of Independent Variables", out="Tables/Spain_cor.tex", no.space=TRUE, label="tab:Spain_cor", digits=2)

### Results belonging to Figure 1----------
model_base <- glm(populist_vote ~zero1(A_critical) + zero1(open) +zero1(con)+ zero1(ext) +zero1(neu) +female +age , data=data, family=binomial)
cl.cov_base <- cluster.vcov(model_base, data$CCAA) # cluster-robust SEs for ols1
cl.robust.se.base <- sqrt(diag(cl.cov_base))

model1 <- glm(populist_vote ~zero1(A_critical) + zero1(open) +zero1(con)+ zero1(ext) +zero1(neu) +female +age + income + income_missing + edu2 + edu3 + edu4 + edu5 + edu6 + zero1(lr_placement) + zero1(cynicism), data=data, family=binomial)
cl.cov1 <- cluster.vcov(model1, data$CCAA) # cluster-robust SEs for ols1
cl.robust.se.1 <- sqrt(diag(cl.cov1))

labels <- c("Agreeableness", "Openness", "Conscientiousness", "Extraversion", "Neuroticism", "Female", "Age",  "Education (Ref: primary): Lower secondary", "Upper secondary", "Post secondary", "Tertiary", "Education missing", "Income", "Income missing", "Left-right ideology",  "Political cynicism")
stargazer(model_base, model1, se=list(cl.robust.se.base, cl.robust.se.1), title="Spanish Election Study: Support for Podemos", align=TRUE, omit.stat=c("LL","ser","f", "adj.rsq"),  star.cutoffs=c(0.1, 0.05), covariate.labels = labels, dep.var.labels.include = FALSE, model.numbers= FALSE, font.size = "tiny",column.labels = c("Base", "Figure 1"), notes = "Unstandardized coefficients (logit) and standard errors (clustered at region); *p<.1, **p<.05",  notes.append = FALSE, out="Tables/Spanish_results.tex", no.space=TRUE, label="tab:Spain_results")

low  <- mean(zero1(data$A_critical), na.rm=T)-sd(zero1(data$A_critical), na.rm=T)
high <- mean(zero1(data$A_critical), na.rm=T)+sd(zero1(data$A_critical), na.rm=T)

odds.difference <- (exp(model1$coefficients)[2]*low) / (exp(model1$coefficients)[2]*high)

### Descriptive statistics---------------
desc.labels <- c("Populist Vote", labels)
stargazer(model1$model, covariate.labels=desc.labels, type = "latex", summary.stat = c("mean", "sd", "median","min",  "max"), title="Descriptive statistics Spanish Election Study", out="Tables/Spain_descrip.tex", no.space=TRUE, label="tab:Spain_descriptives", digits=2)

#Correlation Agreeableness
cor.test(data$A_critical, data$A_empathy)

#Alpha Openness
alpha_o<-data.frame(data$O_artistic, data$O_imagination)
psych::alpha(alpha_o)
#Alpha Conscientiousness
alpha_c<-data.frame(data$C_lazy, data$C_getthingsdone)
psych::alpha(alpha_c)
#Alpha Extraversion
alpha_e<-data.frame(data$E_extravert, data$E_reserved)
psych::alpha(alpha_e)
#Alpha Neuroticism
alpha_n<-data.frame(data$N_stressed, data$N_nervous)
psych::alpha(alpha_n)
